package com.yzg.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzg.pojo.User;
import com.yzg.service.RoleService;
import com.yzg.service.UserService;
import com.yzg.utils.MD5;
import com.yzg.utils.R;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: decade
 * @Description: 用户管理
 * @date: 2021/1/14 21:52
 */
@Api(tags = "UserController", description = "用户管理")
@RestController
@RequestMapping("/acl/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @GetMapping("/{page}/{pageSize}")
    @ApiOperation(value = "获取用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页",required = true,paramType = "Integer"),
            @ApiImplicitParam(name = "pageSize",value = "当前页大小",required = true,paramType = "Integer"),
            @ApiImplicitParam(name = "userQueryVo",value = "用户查询条件",required = false,paramType = "User")
    })
    public R getUserList(@PathVariable Integer page,@PathVariable Integer pageSize,
                         User userQueryVo){
        IPage<User> pageParam = new Page<>(page,pageSize);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if(StringUtils.hasLength(userQueryVo.getUsername())){
            wrapper.like("username",userQueryVo.getUsername());
        }
        userService.page(pageParam,wrapper);
        return R.ok().data("items",pageParam.getRecords()).data("total",pageParam.getTotal());
    }

    @ApiOperation(value = "添加管理用户")
    @PostMapping("/save")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user",value = "用户信息",required = true,paramType = "User")
    })
    public R save(@RequestBody User user){
        user.setPassword(MD5.encrypt(user.getPassword()));
        userService.save(user);
        return R.ok();
    }

    @ApiOperation(value = "根据id获取用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id",required = true,paramType = "String")
    })
    @GetMapping("/getUser/{id}")
    public R getById(@PathVariable String id){
        User user = userService.getById(id);
        return R.ok().data("item",user);
    }

    @ApiOperation(value = "修改管理用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user",value = "用户信息",required = true,paramType = "User")
    })
    @PutMapping("/update")
    public R updateById(@RequestBody User user){
        userService.updateById(user);
        return R.ok();
    }

    @ApiOperation(value = "删除管理用户")
    @DeleteMapping("/remove/{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id",required = true,paramType = "String")
    })
    public R removeById(@PathVariable String id){
        userService.removeById(id);
        return R.ok();
    }

    @ApiOperation(value = "根据id列表删除管理用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "idList",value = "用户id数组",required = true,paramType = "List<String>")
    })
    @DeleteMapping("/batchRemove")
    public R batchRemove(
            @ApiParam(name = "idList", value = "id集合",required = true)
            @RequestBody List<String> idList
    ){
        userService.removeByIds(idList);
        return R.ok();
    }

    @ApiOperation(value = "根据用户id获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public R toAssign(
            @ApiParam(name = "userId", value = "用户id",required = true)
            @PathVariable String userId
    ){
        Map<String, Object> roleMap = roleService.findRoleByUserId(userId);
        return R.ok().data(roleMap);
    }

    @ApiOperation(value = "根据用户分配角色")
    @PutMapping("/doAssign")
    public R doAssign(
            @ApiParam(name = "userId", value = "用户id",required = true)
            @RequestParam String userId,
            @ApiParam(name = "roleIds", value = "角色id数组",required = true)
            @RequestParam String[] roleIds
    ){
        roleService.saveUserRoleRealtionShip(userId,roleIds);
        return R.ok();
    }
}
